package com.lanou.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lanou.entity.OrdBscInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单处理
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    //访问路径
    private static final String HOST_URL = "http://10.90.87.177:8080";
    @Autowired
    private RestTemplate restTemplate;


    /**
     * 分页显示 订单信息(根据session中存储的登录VIP信息)
     * 并跳转页面
     *
     * @return
     */
    @RequestMapping(value = "/getOrder.do", produces = "application/json;charset=utf-8")
    public ModelAndView getOrder(@RequestParam Map<String, String> params) {
        ModelAndView modelAndView = new ModelAndView("view/order");
        //伪造数据
        params.put("vipNo", "00001");

        MultiValueMap bodyMap = new LinkedMultiValueMap();
        bodyMap.setAll(params);
        //跳转到后台Controller
        PageInfo<OrdBscInfo> pageInfo = restTemplate.postForObject(HOST_URL + "/order/getOrder.do", bodyMap, PageInfo.class);
        //没有数据
        if (pageInfo == null) {
            return modelAndView;
        }
        //返回数据
        modelAndView.addObject("page", pageInfo);
        //返回参数
        modelAndView.addObject("params", params);
        return modelAndView;
    }

    /**
     * 订单详情
     */
    @RequestMapping(value = "/prdDtl.do")
    public ModelAndView prdDtl(String ordNo, String vipNo) {
        System.out.println(ordNo + ", " + vipNo);
        ModelAndView modelAndView = new ModelAndView("view/prdDtl");
        List<OrdBscInfo> list = new ArrayList<>();
//        list.add(new OrdBscInfo("王五"));
        modelAndView.addObject("list", list);
        return modelAndView;
    }


    /**
     * 点击签收 更改状态为签收
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/addSignin.do", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> addSignin(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        //伪造数据
        params.put("vipNo", "00001");
        MultiValueMap bodyMap = new LinkedMultiValueMap();
        bodyMap.setAll(params);
        //跳转后台Controller
        return restTemplate.postForObject(HOST_URL + "/order/addSignin.do", bodyMap, Map.class);
    }

    /**
     * 取消订单,修改商品库存
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateOrder.do", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> updateOrder(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        //伪造数据
        params.put("vipNo", "00001");
        params.put("vipId", "1");

        MultiValueMap bodyMap = new LinkedMultiValueMap();
        bodyMap.setAll(params);
        //跳转后台Controller
        return restTemplate.postForObject(HOST_URL + "/order/updateOrder.do", bodyMap, Map.class);
    }

    /**
     * 支付
     * @param params
     * @return
     */
    @RequestMapping(value = "/vipPay.do", produces = "application/json;charset=utf-8")
    public String vipPay(@RequestParam Map<String, Object> params) {
        System.out.println("支付>>>>>>>>>>>>>>");
        System.out.println(params);
        MultiValueMap bodyMap=new LinkedMultiValueMap();
        bodyMap.setAll(params);
        //跳转到后台Controller
        restTemplate.postForObject(HOST_URL+"/order/vipPay.do",bodyMap,String.class);

        return "view/pay";
    }

    /**
     * 添加发票
     *
     */
}
